#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7, maxm = 2e5 + 7, mod = 998244353;
typedef long long int ll;
#define eb emplace_back
#define ef emplace_front
#define ep emplace
#define fi first
#define se second
int n, k;
ll a[maxn], b[maxn], ans;
inline void chang(int x) {
    if (x - k < 1) return;
    //	cout<<x<<' '<<b[x]<<'\n';
    b[x - k] += b[x];
    ans += b[x];
    b[x] = 0;
    if (b[x - k] > 0) chang(x - k);
}
inline int solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        b[i] = a[i] - a[i - 1];
    }
    for (int i = 2; i <= k; ++i)
        if (b[i] > 0) return cout << "-1\n", 0;
    ans = 0;
    //	for(int i=1; i<=n; ++i) cout<<b[i]<<' ';
    //	cout<<'\n';
    for (int i = 2; i <= n; ++i) {
        //		cout<<"This:\n";
        //		for(int i=1; i<=n; ++i) cout<<b[i]<<' ';
        //		cout<<'\n';
        if (b[i] == 0) continue;
        if (b[i] > 0)
            chang(i);
        else {
            ans -= b[i];
            b[i + k] += b[i];
            b[i] = 0;
        }
        //		for(int i=1; i<=n; ++i) cout<<b[i]<<' ';
        //		cout<<'\n';
        //		cout<<"end\n";
    }
    for (int i = 2; i <= n; ++i)
        if (b[i] != 0) return cout << "-1\n", 0;
    cout << ans << '\n';
    return 0;
}
int main() {
    	freopen("in.txt","r",stdin);
    	freopen("wa.txt","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}
